package com.controller;

import com.entity.ElderMonthlyFinance;
import com.service.ElderMonthlyFinanceService;
import com.dto.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("/api/elder/monthly/finance")
public class ElderMonthlyFinanceController {

    @Autowired
    private ElderMonthlyFinanceService elderMonthlyFinanceService;

    @PostMapping
    public void create(@RequestBody ElderMonthlyFinance elderMonthlyFinance) {
        elderMonthlyFinanceService.save(elderMonthlyFinance);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable(value = "id") Integer id) {
        elderMonthlyFinanceService.deleteById(id);
    }

    @PutMapping("/{id}")
    public void update(@PathVariable(value = "id") Integer id, @RequestBody ElderMonthlyFinance elderMonthlyFinance) {
        elderMonthlyFinance.setId(id);
        elderMonthlyFinanceService.save(elderMonthlyFinance);

    }

    @GetMapping("/{id}")
    public ElderMonthlyFinance detail(@PathVariable(value = "id") Integer id) {
        return elderMonthlyFinanceService.findById(id).orElse(null);
    }

    @GetMapping("/page")
    public Page<ElderMonthlyFinance> page(@RequestParam(defaultValue = "1") int pageNum,
                       @RequestParam(defaultValue = "10") int pageSize) {
        return elderMonthlyFinanceService.findAll(pageNum, pageSize);

    }
}